package dssd.configuraciones;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SystemPropertiesLoader  implements ServletContextListener{

	private static final String DSSD_PROPERTIES_LOCATION = "DSSD_PROPERTIES_LOCATION";
	
	private ServletContextEvent servlet = null;

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		this.servlet = arg0;
		String location = System.getenv(DSSD_PROPERTIES_LOCATION);
		if (location != null) {
			Properties properties = new Properties();
			try {
				properties.load(new FileInputStream(new File(location)));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (Object key : properties.keySet()) {
				System.setProperty(key.toString(), properties.getProperty(key.toString().trim()));
			}
		}
	
	}
}